// 线程安全的队列。
#pragma once
#include <mutex>
#include <queue>
#include "../include/obj.h"
#include "semaphore.hpp"

class CSafeQueue : public CVirtualDestruct
{
public:
    CSafeQueue() { Construct(); };
    virtual ~CSafeQueue() { Destruct(); };

    virtual int Init(int iMax = 10);
    /*初始化
    iMax	最大队列长度
    */
    virtual int Recycle();

    virtual int Push(void* pvData);

    virtual int Pop(OUT void*& pvData, int iTimeout = 0);
    virtual int PopAll(STD_ARRAY<void*>* pobjOut = nullptr);

    virtual int Clear() { return PopAll(nullptr); };

protected:
    CSemaphore m_clSem;
    queue<void*> m_objQueue;
    recursive_mutex m_objLock;

    int Construct() { return 0; };
    int Destruct();
};